package com.ruoyi.car.business.service.impl;


import com.ruoyi.car.business.domain.Appointment;
import com.ruoyi.car.business.domain.Statement;
import com.ruoyi.car.business.mapper.AppointmentMapper;
import com.ruoyi.car.business.query.AppointmentQuery;
import com.ruoyi.car.business.service.IAppointmentService;
import com.ruoyi.car.business.service.IStatementService;
import com.ruoyi.car.business.util.NewUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        // 拿到页码,每页显示数量
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        if (appointment != null) {
            // 创建一个新的对象接收前台传入数据
            Appointment newAppointment = new NewUtil().getNewAppointment(appointment);
            newAppointment.setCreateTime(new Date());
            appointmentMapper.insert(newAppointment);
        }

    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        // 判断是否为空 且要在预约状态中才可以修改
        Appointment oldObj = this.get(appointment.getId());
        if (appointment != null && Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            // 创建一个新的对象接收前台传入数据
            Appointment newAppointment = new NewUtil().getNewAppointment(appointment);
            newAppointment.setId(appointment.getId());
            appointmentMapper.updateByPrimaryKey(newAppointment);
        } else {
            throw new ServiceException("只有预约状态才能做修改操作");
        }
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrival(Long id) {
        // 只有预约中才可以进行到店操作
        if (Appointment.STATUS_APPOINTMENT.equals(this.get(id).getStatus())) {
            appointmentMapper.arrival(id, Appointment.STATUS_ARRIVAL, new Date());
        } else {
            throw new ServiceException("只有预约状态才能到店操作");
        }
    }

    @Override
    public void cancel(Long id) {
        // 只有在预约中可以取消
        if (Appointment.STATUS_APPOINTMENT.equals(this.get(id).getStatus())) {
            appointmentMapper.changeStatus(id, Appointment.STATUS_CANCEL);
        }else {
            throw new ServiceException("预约状态中才能取消");
        }
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        // 获取Appointment对象
        Appointment appointment = this.get(appointmentId);
        // 判断是否是已到店 或者结算单生成的客户 才可以生成结算单
        if (Appointment.STATUS_ARRIVAL.equals(appointment.getStatus()) ||
                Appointment.STATUS_SETTLE.equals(appointment.getStatus())||
                Appointment.STATUS_PAY.equals(appointment.getStatus())) {
            // 根据预约单ID查询结算单对象
            Statement statement = statementService.getByAppointmentId(appointmentId);
            // 当对象不为空时 接收前台传入的数据
            if (statement == null) {
                statement = new Statement();
                statement.setCustomerName(appointment.getCustomerName());
                statement.setCustomerPhone(appointment.getCustomerPhone());
                statement.setActualArrivalTime(appointment.getActualArrivalTime());
                statement.setLicensePlate(appointment.getLicensePlate());
                statement.setCarSeries(appointment.getCarSeries());
                statement.setServiceType(appointment.getServiceType());
                statement.setAppointmentId(appointmentId);
                statement.setInfo(appointment.getInfo());
                statement.setCreateTime(new Date());
                statementService.saveFromAppointment(statement);
                // 修改预约单的状态
                appointmentMapper.changeStatus(appointmentId, Appointment.STATUS_SETTLE);
            }
            return statement;
        } else {
            throw new ServiceException("非法操作");
        }
    }

    @Override
    public void changeStatus(Long appointmentId, Integer status) {
        appointmentMapper.changeStatus(appointmentId,status);
    }
}
